package com.irs.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.irs.mapper.TbArticleMapper;
import com.irs.pojo.ArticleSearch;
import com.irs.pojo.TbAdmin;
import com.irs.pojo.TbArticle;
import com.irs.pojo.TbArticleExample;
import com.irs.pojo.TbArticleExample.Criteria;
import com.irs.pojo.TbCategory;
import com.irs.service.AdminService;
import com.irs.service.ArticleService;
import com.irs.service.CategoryService;
import com.irs.util.ResultUtil;

@Service
public class ArticleServiceImpl implements ArticleService{
	@Autowired
	private TbArticleMapper tbArticleMapper;
	@Autowired
	private CategoryService CategoryServiceImpl;
	@Autowired
	private AdminService adminServiceImpl;
	@Override
	public int insService(TbArticle tbArticle) {
		TbAdmin user = (TbAdmin) SecurityUtils.getSubject().getPrincipal();
		tbArticle.setAuthorId(user.getId().toString());
		if(user.getRoleId()==1){
			tbArticle.setStatus(Long.parseLong("1"));
		}else{
			tbArticle.setStatus(Long.parseLong("0"));
		}
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		tbArticle.setCreateTime(df.format(new Date()));
		tbArticle.setUpdateTime(df.format(new Date()));
		int a = tbArticleMapper.insert(tbArticle);
		if(a>0){
			return 1;	
		}else{
			return 0;
		}
		
		
	}
	@Override
	public ResultUtil selArticle(Integer page, Integer limit, ArticleSearch search) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page, limit);
		TbArticleExample example = new TbArticleExample();
		//设置按创建时间降序排序
		example.setOrderByClause("id DESC");
		Criteria criteria = example.createCriteria();
		//根据文章标题进行模糊查询
		System.out.println(search.getTitle());
		if(search.getTitle()!=null&&!"".equals(search.getTitle())){
			criteria.andTitleLike("%"+search.getTitle()+"%");
		}
		//根据分类进行查询
		if(search.getcId()!=null&&!"-1".equals(search.getcId())){
			criteria.andCIdEqualTo(search.getcId());
		}
		
		//根据文章状态进行查询
		if(search.getStatus()!=null&&-1!=(search.getStatus())){
			criteria.andStatusEqualTo(search.getStatus());
		}
		if((search.getCreateTimeStart()!=null&&!"".equals(search.getCreateTimeStart()))&&(search.getCreateTimeEnd()!=null&&!"".equals(search.getCreateTimeEnd()))){
			criteria.andCreateTimeBetween(search.getCreateTimeStart(), search.getCreateTimeEnd());
		}
		List<TbArticle> list = tbArticleMapper.selectByExample(example);
		for (TbArticle tbArticle : list) {
			TbCategory tbCategory = CategoryServiceImpl.selCategoryById(tbArticle.getcId());
			TbAdmin tbAdmin = adminServiceImpl.selAdminById(Long.parseLong(tbArticle.getAuthorId()));
			if((tbCategory.getId().toString()).equals(tbArticle.getcId())){
				tbArticle.setcId(tbCategory.getCategory());		
			}
			if((tbAdmin.getId().toString()).equals(tbArticle.getAuthorId())){
				tbArticle.setAuthorId(tbAdmin.getFullname());
			}
			
		}
		PageInfo<TbArticle> pageInfo = new PageInfo<TbArticle>(list);
		ResultUtil resultUtil = new ResultUtil();
		resultUtil.setCode(0);
		resultUtil.setCount(pageInfo.getTotal());
		resultUtil.setData(pageInfo.getList());
		return resultUtil;
	}
	@Override
	public void deleteById(String id) {
		tbArticleMapper.deleteById(Long.parseLong(id));
	}
	@Override
	public TbArticle selArticleById(String id) {
		// TODO Auto-generated method stub
		
		return tbArticleMapper.selectById(Long.parseLong(id));
	}
	@Override
	public int updateById(TbArticle tbArticle) {
		// TODO Auto-generated method stub
		TbArticle article = tbArticleMapper.selectById(tbArticle.getId());
		TbAdmin user = (TbAdmin) SecurityUtils.getSubject().getPrincipal();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		tbArticle.setUpdateTime(df.format(new Date()));
		tbArticle.setStatus(article.getStatus());
		tbArticle.setAuthorId(user.getId().toString());
		int result = tbArticleMapper.updateById(tbArticle);
		if(result >0){
			return 1;
		}else{
			return 0;
		}

	}
	@Override
	public boolean deleteAll(String idList) {
		// TODO Auto-generated method stub
		String[] ids = idList.split(",");
		if(ids.length>0 && ids!=null){
			for(String id:ids){
				tbArticleMapper.deleteById(Long.parseLong(id));
			}
			return true;
		}else{
			return false;
		}
		
	}
	

}
